<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>获取互联网服务协议对应的端口</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.getservbyname" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">getservbyname</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">getservbyname</span> &mdash; <span class="dc-title">获取互联网服务协议对应的端口</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.getservbyname-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>getservbyname</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$service</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$protocol</code></span>
   ) : <span class="type">int</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>getservbyname()</strong></span> 
   返回互联网服务 <code class="parameter">service</code> 指定的协议 <code class="parameter">protocol</code> 中对应的端口，
   依据 <var class="filename">/etc/services</var>。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.getservbyname-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">service</code></dt>

     <dd>

      <p class="para">
       互联网服务名称的字符串。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">protocol</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">protocol</code> 既可以是 <em>&quot;tcp&quot;</em>
       也可以是 <em>&quot;udp&quot;</em> (小写)。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.getservbyname-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回端口号，如果 <code class="parameter">service</code> 或 <code class="parameter">protocol</code> 未找到返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.getservbyname-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5489">
    <p><strong>Example #1 <span class="function"><strong>getservbyname()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$services&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'http'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'ftp'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'ssh'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'telnet'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'imap'</span><span style="color: #007700">,<br /></span><span style="color: #DD0000">'smtp'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'nicname'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'gopher'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'finger'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'pop3'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'www'</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$services&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$service</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$port&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">getservbyname</span><span style="color: #007700">(</span><span style="color: #0000BB">$service</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'tcp'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$service&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">":&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$port&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.getservbyname-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="getservbyport.html" class="function" rel="rdfs-seeAlso">getservbyport()</a> - Get Internet service which corresponds to port and protocol</span></li>
    <li class="member">
     <a href="http://www.iana.org/assignments/port-numbers" class="link external">&raquo;&nbsp;http://www.iana.org/assignments/port-numbers</a>
     端口号的完整列表
    </li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>